mui.init(); //页面初始化
//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
//全局变量
var username;
var city_leaber = "86";
var phone_type = "domestic";
//查询userinfo接口
function userinfo() {
	mui.ajax(user_url + '/api/userinfo', { //查询用户信息接口
		data: {
			"phone": my_phone,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				var phone = data.resp.phone; //查询手机号
				username = data.resp.username; //用户的username
				$("#phone").val(phone);
				var status = data.resp.status; //账户状态
				if(status == 0) {
					mui.toast('账户已被限制，请联系平台。');
					//					mui.openWindow({
					//						url: '../user/login.html',
					//						createNew: true
					//					})
				}
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
}

//发送验证吗
$(document).on('tap', '#btn', function() {
	var phone = $("#phone").val();
	var region_text_code = $("#region_text_code").text(); //地区区号
	var phone_code = region_text_code.split("+")[1]; //截取
	var phone_sure_code = phone_code.split(")")[0]; //截取
	if(phone_sure_code == "86") {
		phone = $("#phone").val(); //获取注册手机号
	}else{
		phone = phone_sure_code + phone
	}
	mui.ajax(user_url + '/api/verificationCode', {
		data: {
			"phone": phone,
			"sign": "string",
			'username': username,
			"smsChannel": phone_type,
			"user_id": "",
			"type": "FORGET_PAY_PASS_VERIFICATION_CODE"
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		headers: {
			'Content-Type': 'application/json'
		},
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				function invokeSettime(obj) {
					var countdown = 60;
					settime(obj);
					console.log(JSON.stringify(data))

					function settime(obj) {
						if(countdown == 0) {
							$(obj).attr("disabled", false);
							$(obj).text("获取验证码");
							countdown = 60;
							return;
						} else {
							$(obj).attr("disabled", true);
							$(obj).text("重新发送(" + countdown + "s)");
							countdown--;
						}
						setTimeout(function() {
							settime(obj)
						}, 1000)
					}
				}
				new invokeSettime("#btn");
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.toast(error_msg.msg)
		}
	});
})
//忘记支付密码点击下一步
$(document).on('tap', '#next_step', function() {
	var phone = $("#phone").val(); //手机号
	var verification_code = $("#verification_code").val(); //验证码
	if(!verification_code) {
		mui.toast('验证码不能为空');
		return;
	}
	mui.ajax(user_url + '/api/forgetPayPass', {
		data: {
			"phone": username,
			"sign": "string",
			"verification_code": verification_code
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		headers: {
			'Content-Type': 'application/json'
		},
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				mui.openWindow({
					url: 'payment_newpwd.html',
					createNew: true
				})
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.toast(error_msg.msg)
		}
	});
})

//点击跟换地区
$(document).on('tap', '.region_text', function() {
	var region_text = $(this).text(); //地区值
	var region_text_Country = region_text.split("(")[0];
	$("#region_text_Country").text(region_text_Country);
	var region_text_code = region_text.split("(")[1];
	$("#region_text_code").text("(" + region_text_code);
	var city_leabers = $(this).children("a").attr('leaber_num');
	if(city_leabers == "86") {
		phone_type = "domestic";
	} else {
		phone_type = "international";
	}
})
//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		userinfo(); //userin接口
	});
}